package com.example.gateway.config;

import com.example.gateway.filters.CfkServicGateWayFilter0;
import com.example.gateway.filters.CfkServicGateWayFilter1;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.PredicateSpec;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.function.Function;

@Configuration
public class GateWayConfig {

    //注入RouteLocator
    @Bean
    public RouteLocator routeLocator(RouteLocatorBuilder routeLocatorBuilder) {
        RouteLocatorBuilder.Builder builder = routeLocatorBuilder.routes();

        Function<PredicateSpec, Route.AsyncBuilder> xinliangRoute = t -> t.path("/xinliang/**").uri("https://www.sina.com.cn/");

        RouteLocator routeLocator = builder
                .route("path1Id", t -> t.path("/test1/**").uri("https://www.163.com/"))
//                .route("xinliangPathId", xinliangRoute)
                .route("cfk-service-id", t -> t.path("/cfk-service/**")
                                                .filters(f -> f.stripPrefix(1).filter(new CfkServicGateWayFilter0()).filter(new CfkServicGateWayFilter1()))
                        .uri("http://localhost:9090/"))
                .build();
        return routeLocator;
    }

}
